--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function widget:GetInfo()
  return {
    name      = "Unit Spheres",
    desc      = "Draws LOS, Max Range, Hit Sphere, Radar, Sonar, for selected units only.",
    author    = "user",
    date      = "",
    license   = "GNU GPL, v2 or later",
    layer     = -1000,
    enabled   = true  
  }
end

--have fun editing this, DON'T FORGET TO HAVE THAT LIB
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

local quads = "quads"
local lines = "lines"
local points = "points"
local polygon = "polygon"

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

VFS.Include("LuaLibs/Drawing_Lib.lua",nil,VFS.RAW) 

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

local Config = {
     DrawUnits = 1,
     DrawFeatures = 1,
     DrawHitSphere = 1,
     DrawRangeSphere = 0,
     DrawLOSSphere = 0,
     DrawRadarSphere = 0,
     RenderMode = lines,
     UnitFunction = Spring.GetVisibleUnits,
     ShapeNumber = 1,
}

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

local Shapes          = {} 
local shape
local shapenum        = 1  
local Units           = {}
local Features        = {}
local drawunit        = 1
local drawexplosion   = 1
local drawunitrange   = 1 
local drawunitlos     = 1
local drawunitradar   = 1
local drawfeatures    = 0
local drawunits       = 1
local func            = Spring.GetSelectedUnits
local funcnum         = 1 
local funcstring      = "Spring.GetSelectedUnits"
local rendermode      = "quads"

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function widget:DrawWorld() 
   gl.DepthTest(true)
   gl.LineWidth(0.01)
   gl.PointSize(2.5)
 
   for _,unitID in ipairs(Units) do
       local DID = Spring.GetUnitDefID(unitID)  
       local x,y,z = Spring.GetUnitPosition(unitID)
       local urad = Spring.GetUnitRadius(unitID)
       if (not x) then break end   

       if (drawunit == 1) then   
          --start with the solid hitsphere 
          gl.Color(0.9,0.9,0.9,0.25)
          gl.PushMatrix()
          gl.Translate(x,y,z)
          gl.Scale(urad,urad,urad)
          gl.CallList(shape)
          gl.PopMatrix() 
       end 

       if (drawunitlos == 1) then   
          --line of sight sphere
          local losh = (UnitDefs[DID]["losHeight"] or 1)*20 
          local losr = (UnitDefs[DID]["losRadius"] or 1)*20
          gl.Color(0.00,0.70,0.85,0.25)
          gl.PushMatrix()
          gl.Translate(x,y,z)
          gl.Scale(losr,losh,losr)
          gl.CallList(shape)
          gl.PopMatrix() 
       end 

       if (drawunitrange == 1) then   
          --weapon range sphere
          if (UnitDefs[DID]["weapons"][1]) then
             local wep1 = UnitDefs[DID]["weapons"][1]["weaponDef"] or -1
             if (not(wep1 == -1)) then 
                local wrad = WeaponDefs[wep1]["range"]
                local whmod = WeaponDefs[wep1]["heightMod"] or 1
                local wcylt = WeaponDefs[wep1]["cylinderTargetting"]
                gl.Color(0.65,0.10,0.00,0.25)
                gl.PushMatrix()
                gl.Translate(x,y,z)
                gl.Scale(wrad,wrad*whmod,wrad)
                gl.CallList(shape)
                gl.PopMatrix() 
             end
          end
       end 

       if (drawunitradar == 1) then   
          --radar sphere
          local radrad = UnitDefs[DID]["radarRadius"]
          gl.Color(0.00,0.70,0.00,0.25)
          gl.PushMatrix()
          gl.Translate(x,y,z)
          gl.Scale(radrad,radrad,radrad)
          gl.CallList(shape)
          gl.PopMatrix() 
       end 
   end

   for _,featureID in ipairs(Features) do
       local DID = Spring.GetFeatureDefID(featureID)  
       local x,y,z = Spring.GetFeaturePosition(featureID)
       local urad = Spring.GetFeatureRadius(featureID)
       if (not x) then break end   

       --start with the hitsphere 
       gl.Color(0.82,0.82,0.9,0.25)
       gl.PushMatrix()
       gl.Translate(x,y,z)
       gl.Scale(urad,urad,urad)
       gl.CallList(shape)
       gl.PopMatrix()  
   end
   
   gl.Color(1,1,1,1)
   gl.DepthTest(false)
end

function widget:Update()
   if (Spring.GetGameFrame() % 10 < 1) then
      if (drawunits == 1) then Units = func() end
      if (drawfeatures == 1) then Features = Spring.GetAllFeatures() end
   end 
end

function widget:TextCommand(command)
   if (string.find(command, 'drawhitsphere') == 1) then
      if (drawunit == 1) then drawunit = 0  
      elseif (drawunit == 0) then drawunit = 1 end  
   elseif (string.find(command, 'drawrangesphere') == 1) then
      if (drawunitrange == 1) then drawunitrange = 0
      elseif (drawunitrange == 0) then drawunitrange = 1 end  
   elseif (string.find(command, 'drawlossphere') == 1) then
      if (drawunitlos == 1) then drawunitlos = 0
      elseif (drawunitlos == 0) then drawunitlos = 1 end  
   elseif (string.find(command, 'drawradarsphere') == 1) then
      if (drawunitradar == 1) then drawunitradar = 0
      elseif (drawunitradar == 0) then drawunitradar = 1 end 
   elseif (string.find(command, 'renderlines') == 1) then
      rendermode = "lines"
   Shapes[1] = gl.CreateList(gl.Sphere,16,16,1,"quads",0,0,0)
   Shapes[2] = gl.CreateList(gl.Cube,1,rendermode,0,0,0)
   Shapes[3] = gl.CreateList(gl.Cylinder,16,1,4,"quads",0,0,0)
      shape = Sphere
   elseif (string.find(command, 'renderpoints') == 1) then
      rendermode = "points"
      Shapes[1] = gl.CreateList(gl.Sphere,16,16,1,rendermode,0,0,0)
      Shapes[2] = gl.CreateList(gl.Cube,1,rendermode,0,0,0)
      Shapes[3] = gl.CreateList(gl.Cylinder,16,1,4,rendermode,0,0,0)
      shape = Shapes[shapenum]
   elseif (string.find(command, 'renderquads') == 1) then
      rendermode = "quads"
      Shapes[1] = gl.CreateList(gl.Sphere,16,16,1,rendermode,0,0,0)
      Shapes[2] = gl.CreateList(gl.Cube,1,rendermode,0,0,0)
      Shapes[3] = gl.CreateList(gl.Cylinder,16,1,4,rendermode,0,0,0)
      shape = Shapes[shapenum]
   elseif (string.find(command, 'renderpoly') == 1) then
      rendermode = "polygon"
      Shapes[1] = gl.CreateList(gl.Sphere,16,16,1,rendermode,0,0,0)
      Shapes[2] = gl.CreateList(gl.Cube,1,rendermode,0,0,0)
      Shapes[3] = gl.CreateList(gl.Cylinder,16,1,4,rendermode,0,0,0)
      shape = Shapes[shapenum]
   elseif (string.find(command, 'toggleunitsel') == 1) then
      if (funcnum == 1) then func = Spring.GetVisibleUnits; funcstring = "Spring.GetVisibleUnits"   
      elseif (funcnum == 0) then func = Spring.GetSelectedUnits; funcstring = "Spring.GetSelectedUnits" end
   elseif (string.find(command, 'toggledrawfeatures') == 1) then
      if (drawfeatures == 1) then drawfeatures = 0  
      elseif (drawfeatures == 0) then drawfeatures = 1 end  
   elseif (string.find(command, 'toggledrawunits') == 1) then
      if (drawunits == 1) then drawunits = 0  
      elseif (drawunits == 0) then drawunits = 1 end  
   elseif (string.find(command, 'printconfig') == 1) then
      local e = Spring.Echo
      e("local Config = {")
      e("     DrawUnits = " .. drawunits .. ",")  
      e("     DrawFeatures = " .. drawfeatures .. ",")  
      e("     DrawHitSphere = " .. drawunit .. ",")  
      e("     DrawRangeSphere = " .. drawunitrange .. ",")  
      e("     DrawLOSSphere = " .. drawunitlos .. ",")  
      e("     DrawRadarSphere = " .. drawunitradar .. ",")  
      e("     RenderMode = " .. rendermode .. ",")  
      e("     UnitFunction = " .. funcstring .. ",")  
      e("     ShapeNumber = " .. shapenum .. ",")  
      e("}")     
   elseif (string.find(command, 'loadconfig') == 1) then   
      drawunits = Config["DrawUnits"]
      drawfeatures = Config["DrawFeatures"]
      drawunit = Config["DrawHitSphere"]
      drawunitrange = Config["DrawRangeSphere"]
      drawunitlos = Config["DrawLOSSphere"]
      drawunitradar = Config["DrawRadarSphere"]
      rendermode = Config["RenderMode"]
      func = Config["UnitFunction"]
      shapenum = Config["ShapeNumber"]
      Shapes[1] = gl.CreateList(gl.Sphere,16,16,1,rendermode,0,0,0)
      Shapes[2] = gl.CreateList(gl.Cube,1,rendermode,0,0,0)
      Shapes[3] = gl.CreateList(gl.Cylinder,16,1,4,rendermode,0,0,0)
      shape = Shapes[shapenum]
   elseif (string.find(command, 'drawcube') == 1) then
      shapenum = 2
      shape = Shapes[shapenum]
   elseif (string.find(command, 'drawsphere') == 1) then
      shapenum = 1
      shape = Shapes[shapenum]
   elseif (string.find(command, 'drawcylinder') == 1) then
      shapenum = 3
      shape = Shapes[shapenum]
   elseif (string.find(command, 'resetconfig') == 1) then   
      drawunits = 1
      drawfeatures = 0
      drawunit = 1
      drawunitrange = 1
      drawunitlos = 1
      drawunitradar = 1
      rendermode = quads
      func = Spring.GetSelectedUnits
      shapenum = 1
       Shapes[1] = gl.CreateList(gl.Sphere,16,16,1,"quads",0,0,0)
      Shapes[2] = gl.CreateList(gl.Cube,1,rendermode,0,0,0)
      Shapes[3] = gl.CreateList(gl.Cylinder,16,1,4,"quads",0,0,0)
      shape = Shapes[shapenum]
   end 
end

function widget:Initialize()
   Shapes[1] = gl.CreateList(gl.Sphere,16,16,1,"quads",0,0,0)
   Shapes[2] = gl.CreateList(gl.Cube,1,rendermode,0,0,0)
   Shapes[3] = gl.CreateList(gl.Cylinder,16,1,4,"quads",0,0,0)
   Shapes[4] = gl.CreateList(gl.Icosasphere,1,1,1,1,false,2)
   shape = Shapes[shapenum]
end

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
